package com.starhub.application.model.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.starhub.application.interfaces.model.ModelConfigService;
import com.starhub.application.model.enums.ModelTypeEnums;
import com.starhub.application.model.enums.VectorModelTypeEnum;
import com.starhub.common.bean.model.ModelConfig;

/**
 * 向量维度工具类
 * @version: V1.0
 * @author: liuhf
 * @time 2025-08-08
 */
@Component
public class DimensionUtil {

    private static ModelConfigService modelConfigService;

    @Autowired
    public void setModelConfigService(ModelConfigService modelConfigService) {
        DimensionUtil.modelConfigService = modelConfigService;
    }

    /**
     * 获取向量维度
     * @param mark 模型标识
     * @return 向量维度
     */
    public static int getDimension(String mark) {
        if (modelConfigService == null) {
            throw new IllegalStateException("ModelConfigService未初始化");
        }

        ModelConfig config = modelConfigService.getModel(mark);
        return _getDimension(config);
    }

    /**
     * 获取向量维度
     * @param config 模型配置
     * @return 向量维度
     */
    public static int _getDimension(ModelConfig config) {
        if (config == null || !config.getModelType().equals(ModelTypeEnums.EMBEDDING.getCode())) {
            throw new IllegalArgumentException("模型配置异常");
        }

        if (config.getChatType().equals(VectorModelTypeEnum.Custom.getCode())) {
            return config.getDimension();
        }
        return VectorModelTypeEnum.getByCode(config.getChatType()).getDimension();
    }
}
